package com.example.administrator.schoolblog;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.app.ActionBar;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.example.administrator.schoolblog.Beans.UserContent;
import com.zzhoujay.richtext.RichText;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;
import de.hdodenhof.circleimageview.CircleImageView;

public class DetailsActivity extends BaseActivity {
    private String contentId;
    private String userNickname;
    private String userIconUrl;
    private String coverUrl;
    private CollapsingToolbarLayout collapsingToolbar;
    private ImageView coverImage;
    private CircleImageView userIcon;
    private TextView userName;
    private TextView content;
    private LinearLayout detail_user;
    private String userId;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.blog_content_details);
        Bmob.initialize(this,"b5d62a501dc94902d3f8672a1b5fc2ab","Bmob");
        Window window = getWindow();
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            window.setStatusBarColor(Color.TRANSPARENT);
        }
        initView();
    }
    private void initView(){
        Intent intent = getIntent();
        contentId = intent.getStringExtra("contentId");
        userId = intent.getStringExtra("userId");
        Toolbar toolbar = (Toolbar) findViewById(R.id.blog_toolbar);
        setSupportActionBar(toolbar);
        ActionBar actionBar = getSupportActionBar();
        if(actionBar!=null){
            actionBar.setDisplayHomeAsUpEnabled(true);
        }
        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.blog_detail_collapsing);
        coverImage = (ImageView) findViewById(R.id.blog_detail_image);
        userIcon = (CircleImageView) findViewById(R.id.blog_details_user_icon);
        userName = (TextView) findViewById(R.id.blog_detail_userNickname);
        content = (TextView) findViewById(R.id.blog_detail_content);
        detail_user = (LinearLayout) findViewById(R.id.blog_detail_user);
        detail_user.setOnClickListener(new OnClickListener());
        BmobQuery<UserContent> content_detail = new BmobQuery<UserContent>();
        content_detail.getObject(contentId, new QueryListener<UserContent>() {
            @Override
            public void done(UserContent userContent, BmobException e) {
                if (e==null){
                    coverUrl = userContent.getCoverUrl();
                    String title = userContent.getTitle();
                    userIconUrl = userContent.getUserIconUrl();
                    userNickname = userContent.getUserNickname();
                    String content_detail = userContent.getContent();
                    if (coverUrl!=null){
                        Glide.with(DetailsActivity.this).load(coverUrl).into(coverImage);
                    }else {
                        Glide.with(DetailsActivity.this).load(R.drawable.blog_content_bg).into(coverImage);
                    }
                    if(userIconUrl!=null){
                        Glide.with(DetailsActivity.this).load(userIconUrl).into(userIcon);
                    }
                    RichText.from(content_detail).into(content);
                    userName.setText(userNickname);
                    collapsingToolbar.setTitle(title);
                }
            }
        });
    }
    public class OnClickListener implements View.OnClickListener{
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.blog_detail_user:
                    Intent other = new Intent(DetailsActivity.this,OtherinfoActivity.class);
                    other.putExtra("userId",userId);
                    //Toast.makeText(DetailsActivity.this, userId, Toast.LENGTH_SHORT).show();
                    startActivity(other);
                    break;
            }
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
